@extends("base.base")
@include('User.Order.header')
@section('main')
    <link rel="stylesheet" href="{{asset(env('CDN_HOST')."/public/css/order/list.css")}}">
    <main id="main" class="main">
        @if(empty($parent_order))
            <div id="empty_order" class="empty_order">
                <img class="empty_order-img" src="{{asset(env("CDN_HOST")."/img/cart/cart.png")}}" alt="">
                <p class="empty_order-p">去买点儿东西吧</p>
            </div>
        @else
            <div id="order_list" class="order_list">
                @foreach($parent_order as $key => $value)
                    <div class="parent_order_box" data-order-id="{{$value["parent_order_id"]}}">
                        <div class="order_title">
                                <span class="order_number">
                                    订单编号：{{$value["parent_order_id"]}}
                                </span>
                            <span class="order_delete">
                                    <i class="fa fa-trash-o close_order" data-order-id="{{$value["parent_order_id"]}}"
                                       data-state="{{$value["state"]}}" aria-hidden="true"></i>
                                </span>
                        </div>
                        <div class="order_content">
                            <a href="/user/order/{{$value["parent_order_id"]}}">
                                <div class="order_box">
                                    @foreach($value["data"] as $k => $v)
                                        <div class="img_box" data-order-id="{{$v["order_id"]}}">
                                            <img class="order_img" src="{{$v["info_img_url"]}}" alt="">
                                        </div>
                                    @endforeach
                                </div>
                            </a>
                            <div class="order_state">
                                <span class="order_state_name" data-order-id="{{$value["parent_order_id"]}}"
                                      data-state="{{$value["state"]}}">{{$value["state_name"]}}</span>
                            </div>
                        </div>
                        <div class="order_bottom">
                            订单生成时间: {{$value["created_at"]}}
                        </div>
                    </div>
                @endforeach
            </div>
        @endif
    </main>
    <script>
        var state_admin = <?=json_encode($state, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)?>;
        $(".order_state_name").on("click", function () {
            loading.show()
            var th = $(this)
            var state = parseInt(th.attr("data-state"))
            var parent = th.parents(".parent_order_box")
            var parent_order_id = parent.attr("data-order-id")
            if (parent_order_id == "" || typeof parent_order_id == "undefined") {
                return false
            }
            parent_order_id = parseInt(parent_order_id)
            switch (state) {
                case 0:
                    pay(th, parent_order_id)
                    break
                case 1:
                    alert("已通知商家")
                    break
                case 2:
                case 3:
                    add_state(th, parent_order_id, state)
                    break
                default:
                    loading.hide()
                    break
            }
        })

        function pay(jq, parent_order_id) {
            window.location.href = "/order/pay/" + parent_order_id
            loading.hide()

        }

        function add_state(th, id, state) {
            var data = {
                id: id,
                state: state,
                _token: csrf_token
            }
            $.post("/api/add_state", data, function (e) {
                if (e.errorCode == 0) {
                    window.location.reload()
                } else {
                    alert(e.errorMsg);
                }
                loading.hide()
            });
        }

        function change_state_text(th, state) {
            th.attr("data-state", state)
            th.text(state_admin[state])
        }

        $(".close_order").on("click", function () {
            loading.show()
            var th = $(this)
            var parent_order_id = parseInt(th.attr("data-order-id"))
            var state = parseInt(th.attr("data-state"))
            if (state == 6)
                delete_order(th, parent_order_id)
            else
                close_order(th, parent_order_id)
        })

        function close_order(th, id) {
            var data = {
                id: id,
                _token: csrf_token
            }
            $.post("/api/close_order", data, function (e) {
                if (e.errorCode == 0) {
                    window.location.reload()
                } else {
                    alert(e.errorMsg);
                }
                loading.hide()
            });
        }

        function delete_order(th, id) {
            var data = {
                id: id,
                _token: csrf_token
            }
            $.post("/api/delete_order", data, function (e) {
                if (e.errorCode == 0) {
                    window.location.reload()
                } else {
                    alert(e.errorMsg);
                }
                loading.hide()
            });
        }
    </script>
@endsection